/*
 * The National Parks Database Project
 * Group: Tyler Simrell, Vladmir Dudnic, Michael Jagielo
 * UpdateDBPanel.java
 */

package panels;

import gui.MainFrame;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import database.DBInterface;

/**
 * This JPanel creates the Update panel that allows the user to update the information of a trail.
 * @author Tyler Simrell
 * @version 5/24/13
 */
@SuppressWarnings("serial")
public class UpdateDBPanel extends JPanel implements ActionListener
{

	private CardLayout cards;
	private JPanel cardPanel;
	private JLabel updateLabel;
	private JTextField parkNameField;
	private JTextField vistaPointField;
	private JTextField slopeField;
	private JTextField lengthField;
	private JComboBox<String> terrainComboBox;
	private JComboBox<String> difficultyComboBox;
	private String trailName;
	
	/**
	 * This constructor takes in the parameters to allow this panel to be added to the cards layout.
	 * @param passedInCards The card layout that holds many of the panels in this program
	 * @param passedInCardPanel The panel that the card layout sits upon.
	 * @param passedInUpdateLabel The label that the trail name will go on. 
	 */
	public UpdateDBPanel(CardLayout passedInCards, JPanel passedInCardPanel, JLabel passedInUpdateLabel)
	{
		super();
		cards = passedInCards;
		cardPanel = passedInCardPanel;
		updateLabel = passedInUpdateLabel;
		setUp();
	}
	
	/**
	 * This method sets up the UI in this panel
	 */
	private void setUp()
	{
		this.setLayout(null);
		cards.addLayoutComponent(this, MainFrame.UPDATE_DB_PANEL);		
		
		JPanel centeringPanel = new JPanel();
		centeringPanel.setBounds(12, 12, 820, 35);
		this.add(centeringPanel);
		
		updateLabel.setText("Update " + "<Insert Trail Name Here>");
		centeringPanel.add(updateLabel);
		updateLabel.setFont(new Font("Tempus Sans ITC", Font.BOLD, 18));
		
		JLabel parkNameLabel = new JLabel("Park Name");
		parkNameLabel.setBounds(82, 112, 69, 16);
		this.add(parkNameLabel);
		
		parkNameField = new JTextField();
		parkNameField.setBounds(205, 110, 388, 20);
		this.add(parkNameField);
		parkNameField.setColumns(10);
		
		JLabel vistaPointLabel = new JLabel("Vista Points");
		vistaPointLabel.setBounds(82, 156, 69, 16);
		this.add(vistaPointLabel);
		
		vistaPointField = new JTextField();
		vistaPointField.setBounds(205, 154, 211, 20);
		this.add(vistaPointField);
		vistaPointField.setColumns(10);
		
		JLabel slopeLabel = new JLabel("Slope");
		slopeLabel.setBounds(82, 195, 55, 16);
		this.add(slopeLabel);
		
		slopeField = new JTextField();
		slopeField.setBounds(205, 193, 69, 20);
		this.add(slopeField);
		slopeField.setColumns(10);
		
		JLabel lengthLabel = new JLabel("Length");
		lengthLabel.setBounds(82, 238, 55, 16);
		this.add(lengthLabel);
		
		lengthField = new JTextField();
		lengthField.setBounds(205, 236, 69, 20);
		this.add(lengthField);
		lengthField.setColumns(10);
		
		JLabel terrainLabel = new JLabel("Terrain");
		terrainLabel.setBounds(82, 284, 55, 16);
		this.add(terrainLabel);
		
		terrainComboBox = new JComboBox<String>();
		terrainComboBox.setBounds(205, 284, 81, 20);
		terrainComboBox.setToolTipText("The type of terrain that the trail has.");
		terrainComboBox.setModel(new DefaultComboBoxModel<String>(MainFrame.TERRAIN_TYPES));
		this.add(terrainComboBox);
		
		JLabel difficultyLabel = new JLabel("Difficulty");
		difficultyLabel.setBounds(82, 326, 55, 16);
		this.add(difficultyLabel);
		
		difficultyComboBox = new JComboBox<String>();
		difficultyComboBox.setModel(new DefaultComboBoxModel<String>(MainFrame.DIFFICULTY_LEVELS));
		difficultyComboBox.setToolTipText("The overall difficulty of the trail, which is derived from the length, slope, and terrain.");
		difficultyComboBox.setBounds(205, 324, 81, 20);
		this.add(difficultyComboBox);
		
		JButton updateButton = new JButton("Update");
		updateButton.setBounds(371, 405, 98, 26);
		updateButton.addActionListener(this);
		this.add(updateButton);
		
		this.add(new LowerPanel(cards, cardPanel, true));
	}
	
	public void setUpFields(String passedInTrailName)
	{
		trailName = passedInTrailName;
		String query = "SELECT * FROM trail_inventory WHERE trail_name = '" + trailName + "';";
		DBInterface db = new DBInterface();
		ResultSet rs = db.customQuery(query);
		
		try 
		{
			if(!rs.isBeforeFirst())
			{
				JOptionPane.showMessageDialog(null, "Sorry, this trail does not exist in the database", 
						"Database Error!", JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				rs.next();
				parkNameField.setText(rs.getString("park_name"));
				vistaPointField.setText(rs.getString("vista"));
				slopeField.setText(Double.toString(rs.getDouble("slope")));
				lengthField.setText(Double.toString(rs.getDouble("length")));

				String terrainOption = rs.getString("terrain");
				if(terrainOption.equals("Forest"))
				{
					terrainComboBox.setSelectedIndex(0);
				}
				else
				{
					terrainComboBox.setSelectedIndex(1);
				}
				
				String difficultyOption = rs.getString("difficulty");
				if(difficultyOption.equals("Easy"))
				{
					difficultyComboBox.setSelectedIndex(0);
				}
				
				else if(difficultyOption.equals("Medium"))
				{
					difficultyComboBox.setSelectedIndex(1);
				}
				
				else if(difficultyOption.equals("Hard"))
				{
					difficultyComboBox.setSelectedIndex(2);
				}
				
			}
			db.closeConnection();
		} 
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		
	}
	
	/**
	 * This action performed method takes in the given data and updates a tuple with it.
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(parkNameField.getText().isEmpty() || slopeField.getText().isEmpty() || 
				lengthField.getText().isEmpty())
		{
			JOptionPane.showMessageDialog(null, "A Field (besides Vista) Was Left Empty", 
					"Trail Information Error", JOptionPane.ERROR_MESSAGE);
		}
		else
		{
			try
			{
				double slope = Double.parseDouble(slopeField.getText());
				double length = Double.parseDouble(lengthField.getText());
				
				DBInterface db = new DBInterface();
				db.updateInventoryTable(trailName, parkNameField.getText(), 
						vistaPointField.getText(), slope, length, 
						terrainComboBox.getSelectedItem().toString(), 
						difficultyComboBox.getSelectedItem().toString());
				cards.show(cardPanel, MainFrame.MAINTENANCE_PANEL);
			}
			catch(NumberFormatException exception)
			{
				JOptionPane.showMessageDialog(null, "A Field Had a non number when it was " +
						"expecting a number", "Trail Information Error", 
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
